<?php
/**
 * 评论管理
 * $Id: class.comment.php 238 2011-01-13 08:53:47Z bwanglei@gmail.com $
 */

class awComment {
	/**
	 * 内部数据对象
	 * @var MySQL
	 */
	private $db;

	function __construct() {
		$this->db = MySQL::getInstance();
	}

	/**
	 * 获取评论
	 *
	 * @param int $blogId
	 * @param int $page
	 * @return array
	 */
	function getComments($blogId=null, $page=null) {
		$andQuery = '';
		$andQuery .= $blogId ? ' WHERE aid='.$blogId : '';
		$condition = '';
		if ($page) {
			$startId = ($page-1) * ADMIN_PREPAGE_NUM;
			$condition = ' LIMIT '.$startId.','.ADMIN_PERPAGE_NUM;
		}
		$sql = 'SELECT * FROM `'.DB_PREFIX.'comments`'.$andQuery.' ORDER BY id ASC'.$condition;
		$ret = $this->db->query($sql);
		$comments = array();
		while ($row = $this->db->fetch_array($ret)) {
			$row['nickname'] = htmlspecialchars($row['nickname']);
			$row['email'] = htmlspecialchars($row['email']);
			$row['url'] = htmlspecialchars($row['url']);
			$row['content'] = htmlspecialchars($row['content']);
			$comments[] = $row;
		}
		return $comments;
	}
	
	/**
	 * 后台取得评论
	 */
	function get_comment_root($bookmark, $count_page=7) {
		if (!isset($bookmark) || $bookmark=='') {
			$sql = 'SELECT * FROM `'.DB_PREFIX.'comments` ORDER BY `time_date` DESC LIMIT '.($count_page+1);
			$pre_sql = '';
		} else {
			$sql = 'SELECT * FROM `'.DB_PREFIX.'comments` WHERE `time_date`<='.$bookmark.' ORDER BY `time_date` DESC LIMIT '.($count_page+1);
			$pre_sql = 'SELECT * FROM `'.DB_PREFIX.'comments` WHERE `time_date`>'.$bookmark.' ORDER BY `time_date` ASC LIMIT '.$count_page;
		}
		$result_comments = $this->db->query($sql);
		//取得next_bookmark
		$count_result = $this->db->num_rows($result_comments);
		if ($count_result <= $count_page) {
			$next_bookmark = false;
		} else {
			mysql_data_seek($result_comments, $count_result-1);
			$next_bookmark = $this->db->fetch_array($result_comments);
			$next_bookmark = $next_bookmark['time_date'];
			mysql_data_seek($result_comments,0);
		}

		//取得pre_bookmark
		if (isset($pre_sql) && $pre_sql!='') {
			$pre_comments = $this->db->query($pre_sql);
			$count_pre = $this->db->num_rows($pre_comments);
			if ($count_pre == 0) {
				$pre_bookmark = false;
			} else {
				mysql_data_seek($pre_comments, ($count_pre-1));
				$pre_bookmark = $this->db->fetch_array($pre_comments);
				$pre_bookmark = $pre_bookmark['time_date'];
			}
		} else {
			$pre_bookmark = false;
		}
		
		//取得结果集
		$comments = array();
		while ($comment = $this->db->fetch_array($result_comments)) {
			$comments[] = $comment;
		}
		//删除最后一个多余的
		if (count($comments)>$count_page) array_pop($comments);
		return array($comments, $pre_bookmark, $next_bookmark);
	}

	/**
	 * 获取某篇博客的评论数目
	 *
	 * @param int $blogId
	 * @param int $comNum
	 */
	function getCommentNum($blogId=null) {
		$res =$this->db->query('SELECT id FROM `'.DB_PREFIX.'comments` WHERE aid='.$blogId);
		$comNum = $this->db->num_rows($res);
		return $comNum;
	}

	/**
	 * 删除评论
	 *
	 * @param int $commentId
	 */
	function delComment($commentId) {
		$this->db->query('DELETE FROM `'.DB_PREFIX.'comments` WHERE id='.$commentId);
	}

	/**
	 * 批量删除评论
	 * @param string $action
	 * @param array $comments
	 */
	function delComments($commentIds) {
		foreach ($commentIds as $cid) {
			$this->delComment($cid);
		}
	}

	function addComment($name, $mail, $url, $content, $imgcode, $blogId) {
		global $utctimestamp;
		if ($url && strncasecmp($url, 'http',4)) 
			$url = 'http://'.$url;
		$this->setCommentCookie($name,$mail,$url,$utctimestamp);

		if ($this->isLogCanComment($blogId) === false) {
			return -1;
		} elseif (preg_match("/['<>,#|;\/\$\\&\r\t()%@+?^]/",$name) || strlen($name) > 20 || strlen($name) == 0){
			return -2;
		} elseif ($mail!='' && !checkMail($mail)) {
			return -3;
		} elseif ($content=='' || strlen($countent)>2000) {
			return -4;
		} elseif (session_start() && $imgcode != $_SESSION['code']) {
			return -5;
		} else {
			$sql = 'INSERT INTO `'.DB_PREFIX.'comments` (nickname,email,url,content,aid,time_date) VALUES (\''.$name.'\',\''.$mail.'\',\''.$url.'\',\''.$content.'\','.$blogId.','.$utctimestamp.')';
			$ret = $this->db->query($sql);
		}
	}

	function isLogCanComment($blogId) {
		$query = $this->db->query('SELECT `allow_remark` FROM `'.DB_PREFIX.'articles` WHERE id='.$blogId);
		$show_remark = $this->db->fetch_array($query);
		if ($show_remark['allow_remark'] == 'n') return false;// || $show_remark === false) return false;
		return true;
	}

	function setCommentCookie($name,$mail,$url,$utctimestamp) {
		$cookietime = $tuctimeStamp + 31536000;
		setcookie('comNickname',$name,$cookietime);
		setcookie('comEmail',$mail,$cookietime);
		setcookie('comUrl',$url,$cookietime);
	}
}
